<?php
// 筛选数据组件
function renderFilterRecords($conn) {
    // 获取习题集数据
    $exercise_sets = [];
    $result = $conn->query("SELECT id, name FROM exercise_sets ORDER BY name");
    while ($row = $result->fetch_assoc()) {
        $exercise_sets[] = $row;
    }
    
    // 获取唯一值用于筛选下拉菜单
    $unique_types = get_unique_values($conn, 'question_type');
    
    // 获取章节数据（按习题集分组）
    $chapters_by_set = [];
    $result = $conn->query("SELECT c.id, c.name, c.exercise_set_id FROM chapters c ORDER BY c.exercise_set_id, c.name");
    while ($row = $result->fetch_assoc()) {
        if (!isset($chapters_by_set[$row['exercise_set_id']])) {
            $chapters_by_set[$row['exercise_set_id']] = [];
        }
        $chapters_by_set[$row['exercise_set_id']][] = $row;
    }
    
    $filterHtml = '    <div class="card">
            <div class="card-header">
                <h3>筛选数据</h3>
            </div>
            <div class="card-body">
                <form method="get" class="row g-3">
                    <input type="hidden" name="tab" value="list">
                    <div class="col-md-3">
                        <label class="form-label">日期</label>
                        <input type="date" name="filter_date" class="form-control" value="' . (isset($_GET['filter_date']) ? $_GET['filter_date'] : '') . '">
                    </div>
                    
                    <div class="col-md-3">
                        <label class="form-label">做题方式</label>
                        <select name="filter_type" class="form-control form-select shadow-none border border-gray-300 rounded-lg transition-all duration-200 focus:border-primary focus:ring-2 focus:ring-primary/20">
                            <option value="">全部</option>';
    
    foreach ($unique_types as $type) {
        $selected = (isset($_GET['filter_type']) && $_GET['filter_type'] == $type) ? 'selected' : '';
        $filterHtml .= '                            <option value="' . $type . '" ' . $selected . '>' . $type . '</option>';
    }
    
    $filterHtml .= '                        </select>
                    </div>
                    

                    
                    <div class="col-md-3">
                        <label class="form-label">习题集</label>
                        <select name="filter_exercise_set" id="filter_exercise_set" class="form-control form-select shadow-none border border-gray-300 rounded-lg transition-all duration-200 focus:border-primary focus:ring-2 focus:ring-primary/20">
                            <option value="">全部</option>';
    
    foreach ($exercise_sets as $set) {
        $selected = (isset($_GET['filter_exercise_set']) && $_GET['filter_exercise_set'] == $set['id']) ? 'selected' : '';
        $filterHtml .= '                            <option value="' . $set['id'] . '" ' . $selected . '>' . $set['name'] . '</option>';
    }
    
    $filterHtml .= '                        </select>
                    </div>
                    
                    <div class="col-md-3">
                        <label class="form-label">章节</label>
                        <select name="filter_chapter" id="filter_chapter" class="form-control form-select shadow-none border border-gray-300 rounded-lg transition-all duration-200 focus:border-primary focus:ring-2 focus:ring-primary/20">
                            <option value="">全部</option>';
    
    // 获取当前选中的习题集ID
    $selected_set_id = isset($_GET['filter_exercise_set']) ? $_GET['filter_exercise_set'] : '';
    
    // 如果有选中的习题集，显示对应章节
    if ($selected_set_id && isset($chapters_by_set[$selected_set_id])) {
        foreach ($chapters_by_set[$selected_set_id] as $chapter) {
            $selected = (isset($_GET['filter_chapter']) && $_GET['filter_chapter'] == $chapter['id']) ? 'selected' : '';
            $filterHtml .= '                            <option value="' . $chapter['id'] . '" ' . $selected . '>' . $chapter['name'] . '</option>';
        }
    } else if (!$selected_set_id) {
        // 如果没有选中习题集，显示所有章节
        foreach ($chapters_by_set as $chapters) {
            foreach ($chapters as $chapter) {
                $selected = (isset($_GET['filter_chapter']) && $_GET['filter_chapter'] == $chapter['id']) ? 'selected' : '';
                $filterHtml .= '                            <option value="' . $chapter['id'] . '" ' . $selected . '>' . $chapter['name'] . '</option>';
            }
        }
    }
    
    $filterHtml .= '                        </select>
                    </div>
                    
                    <div class="col-md-3">
                        <label class="form-label">正确情况</label>
                        <select name="filter_correctness" class="form-control">
                            <option value="">全部</option>
                            <option value="未批改" ' . (isset($_GET['filter_correctness']) && $_GET['filter_correctness'] == '未批改' ? 'selected' : '') . '>未批改</option>
                            <option value="正确" ' . (isset($_GET['filter_correctness']) && $_GET['filter_correctness'] == '正确' ? 'selected' : '') . '>正确</option>
                            <option value="错误" ' . (isset($_GET['filter_correctness']) && $_GET['filter_correctness'] == '错误' ? 'selected' : '') . '>错误</option>
                            <option value="不完全正确" ' . (isset($_GET['filter_correctness']) && $_GET['filter_correctness'] == '不完全正确' ? 'selected' : '') . '>不完全正确</option>
                        </select>
                    </div>
                    
                    <div class="col-md-3">
                        <label class="form-label">最小耗时(分钟)</label>
                        <input type="number" name="filter_min_duration" class="form-control" min="0" placeholder="最小分钟数" value="' . (isset($_GET['filter_min_duration']) ? $_GET['filter_min_duration'] : '') . '">
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">最大耗时(分钟)</label>
                        <input type="number" name="filter_max_duration" class="form-control" min="0" placeholder="最大分钟数" value="' . (isset($_GET['filter_max_duration']) ? $_GET['filter_max_duration'] : '') . '">
                    </div>
                    
                    <div class="col-12">
                        <div class="d-flex gap-3">
                            <button type="submit" class="btn btn-primary">筛选</button>
                            <a href="index.php?tab=list" class="btn btn-secondary">重置筛选</a>
                            <a href="index.php?export&tab=list" class="btn btn-success">导出Excel</a>
                        </div>
                    </div>
                </form>
            </div>
        </div>';
    
    return $filterHtml;
}

// 构建筛选查询
function buildFilterQuery($conn, $filter_conditions = [], $params = [], $types = '') {
    if (!empty($_GET['filter_date'])) {
        $filter_conditions[] = "e.exam_date = ?";
        $params[] = $_GET['filter_date'];
        $types .= 's';
    }

    if (!empty($_GET['filter_type'])) {
        $filter_conditions[] = "e.question_type = ?";
        $params[] = $_GET['filter_type'];
        $types .= 's';
    }

    if (!empty($_GET['filter_exercise_set'])) {
        $filter_conditions[] = "e.exercise_set_id = ?";
        $params[] = $_GET['filter_exercise_set'];
        $types .= 'i';
    }
    
    if (!empty($_GET['filter_chapter'])) {
        $filter_conditions[] = "e.chapter_id = ?";
        $params[] = $_GET['filter_chapter'];
        $types .= 'i';
    }

    // 耗时范围筛选
    if (isset($_GET['filter_min_duration']) && $_GET['filter_min_duration'] !== '') {
        $filter_conditions[] = "e.duration >= ?";
        $min_duration = intval($_GET['filter_min_duration']);
        $params[] = $min_duration;
        $types .= 'i'; // 明确添加整数类型标记
    }
    if (isset($_GET['filter_max_duration']) && $_GET['filter_max_duration'] !== '') {
        $filter_conditions[] = "e.duration <= ?";
        $max_duration = intval($_GET['filter_max_duration']);
        $params[] = $max_duration;
        $types .= 'i'; // 明确添加整数类型标记
    }
    
    // 正确情况筛选
    if (!empty($_GET['filter_correctness'])) {
        $filter_conditions[] = "e.correctness = ?";
        $params[] = $_GET['filter_correctness'];
        $types .= 's';
    }
    
    return [
        'conditions' => $filter_conditions,
        'params' => $params,
        'types' => $types
    ];
}